package com.actitime.dataprovider;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DataProviderTest {
	public WebDriver driver;
	
	
	@Test(dataProvider="datap")
  public void createCustomer(String Value, String Description) {
		  //creating customer
		  driver.findElement(By.xpath("//div[contains(text(),'Tasks')]")).click();
		  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		  driver.findElement(By.linkText("Projects & Customers")).click();
		  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		  driver.findElement(By.xpath("//input[@value='Create New Customer']")).click();
		  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		  driver.findElement(By.name("name")).sendKeys(Value);
		  driver.findElement(By.name("description")).sendKeys(Description);
		  driver.findElement(By.name("createCustomerSubmit")).click();
		  System.out.println(driver.findElement(By.xpath("//span[@class='successmsg']")).getText());
		  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

	}
	
	@DataProvider(name = "datap")
	public Object[][] getData() throws InvalidFormatException, IOException{
		FileInputStream fis = new FileInputStream("C:\\Test Case\\CustData.xlsx");
		Workbook wb = WorkbookFactory.create(fis);
		Sheet sh = wb.getSheet("Data");
		int rowCount = sh.getLastRowNum();
		System.out.println("Row Count = "+ rowCount);
		Object[][] data = new Object[rowCount][2];
		System.out.println("Size of Object[][] = " +data.length);
		for (int i=1; i<=rowCount;i++){
			Row row = sh.getRow(i);
			String custName= row.getCell(1).getStringCellValue();
			String custDescription =row.getCell(2).getStringCellValue();
			
			data[i-1][0] = custName;
			data[i-1][1] = custDescription;
		}
		return data;
		
	}
	
	
  @BeforeMethod
  public void login()
  {
	 //logging in
	  driver.findElement(By.name("username")).sendKeys("admin");
	  driver.findElement(By.name("pwd")).sendKeys("manager");
	  driver.findElement(By.xpath("//a[@id='loginButton']")).click();
	  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	 
	  }

  @AfterMethod
  public void logout() {
	  driver.findElement(By.linkText("Logout")).click();
  }

  @BeforeClass
  public void BeforeClass() {
	  driver = new FirefoxDriver();
	  driver.get("http://127.0.0.1/login.do");
	   }

  @AfterClass
  public void AfterClass() {
	  driver.quit();
  }

}
